/**
 * Created by Administrator on 2017/3/18 0018.
 */
import React from "react";
import ReplyLastUserMutation from './mutation/ReplyLastUserMutation'
import ReplyDetailMutation from './mutation/ReplyDetailMutation'
import ReplySaveMutation from './mutation/ReplySaveMutation'
import Relay from "react-relay";
import "./css/Tickling.css";
import { Modal, Button } from 'antd';
import InputBox from "../../common/InputBox.js";
import Util from "../../util/Util";

/*导出组件*/
export default class ReplyDetail extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            sid:this.props.params.sid,
            replyData:[],
            userPhone:'',
            userDate:'',
            userName:'',
            label:'',
        };
        this.style = {
            left: {
                width: '96px',
                height: '27px',
                backgroundColor: "#cc3300",
                fontSize: "16px",
                color: "#fff",
                fontFamily: "Microsoft YaHei",
                borderRadius: "3px",
                float:"left"
            },
            right: {
                width: '96px',
                height: '27px',
                backgroundColor: "#33ff66",
                fontSize: "16px",
                color: "#fff",
                fontFamily: "Microsoft YaHei",
                borderRadius: "3px",
                float:"right"
            }
        }
    }

    componentWillMount() {
        // this.getUser();
        this.getDetail(1,10);
    }
    getDetail(pageNum, pageSize) {
        Relay.Store.commitUpdate(
            new ReplyDetailMutation({
                //放传给后台参数的地方
                suggestId:this.state.sid,
                pageNum: pageNum,
                pageSize: pageSize,
            }),
            {
                onFailure: () => {
                },
                onSuccess: (response) => {
                    let data = response.getReplyList.getReplyList;
                    let errorCode=response.getReplyList.getReplyList.errorCode;
                    if(errorCode=="0"){
                        this.setState({
                            totalresult: data.totalResult,
                            replyData:data.replyModelList
                        })
                        this.setState({
                            userName:data.replyModelList[0].userName,
                            userDate:this.dateFormat(data.replyModelList[0].createDate),
                            userPhone:data.replyModelList[0].phone
                        })
                    }
                }
            });
    }
    //
    // getUser(){
    //     Relay.Store.commitUpdate(
    //         new ReplyLastUserMutation({
    //             //放传给后台参数的地方
    //             suggestId:this.state.sid
    //         }),
    //         {
    //             onFailure: () => {
    //             },
    //             onSuccess: (response) => {
    //                 let data = response.getReplyLastUser.getReplyLastUser;
    //                 let errorCode=response.getReplyLastUser.getReplyLastUser.errorCode;
    //
    //                 if(errorCode=="0"){
    //                     if(data.suggestModel!=null){
    //                         this.setState({
    //                             userName: data.suggestModel.userName,
    //                             userDate:this.dateFormat(data.suggestModel.createDate),
    //                             userPhone:data.suggestModel.phone
    //                         })
    //                     }else{
    //                         this.setState({
    //                             userName: data.replyModel.userName,
    //                             userDate:this.dateFormat(data.replyModel.createDate),
    //                             userPhone:data.replyModel.phone,
    //                             label:data.replyModel.label
    //                         })
    //                         // this.state.userName=data.replyModel.userName;
    //                         // this.state.userDate=dateFormat(data.replyModel.createDate);
    //                         // this.state.userPhone=data.replyModel.phone;
    //                     }
    //                     this.setState({
    //                         totalresult: data.totalResult,
    //                         replyData:data.replyModelList
    //                     })
    //                 }
    //             }
    //         });
    // }

    addList(){
        return this.state.replyData.map((item, index) => {
            return (
                <div  key={index + "_"}>
                    <dl data-id={item.sid}>
                        <dt>
                            {
                                item.label==0 ? <img src={require("./img/user_ico2.png")} alt="" /> : <img src={require("./img/user_ico.png")} alt="" />

                            }
                        </dt>
                        <dd>
                            <p>{this.dateFormat(item.createDate)}</p>
                            <p>{item.detail}</p>
                        </dd>
                    </dl>
                </div>
            )
        });
    }

    /*点击确认*/
    handleConfirm(flag) {

        var id=this.state.sid
        var content=this.refs.content.state.text.trim();
        var label=1;
        // debugger;
        if(content === null || content === undefined || content === ''){
            this.refs.contentPoint.textContent="回复内容不允许为空"
            return;
        }else{
            this.refs.contentPoint.textContent=""

        }
        Relay.Store.commitUpdate(
            new  ReplySaveMutation({
                suggestId: id,
                label: label,
                detail: content
            }),
            {
                onFailure: () => {
                },
                onSuccess: (response) => {
                    var data = response.saveReply.saveReply;
                    if (data.errorCode == 0) {
                        this.showInfo("", "回复成功");
                        this.getDetail(1,10);
                        this.refs.content.state.text=""
                    } else {
                        this.showError("error",data.value);
                    }
                }
            }
        )
    }

    /**错误提示框*/
    showError(title, value) {
        Modal.error({
            title: !title ? "提示：" : title,
            content: value,
            width: 390
        });
    }

    /*成功提醒*/
    showInfo(title, value) {
        Modal.info({
            title: !title ? "提示：" : title,
            content: value,
            width: 390
        });
    }

    //asdad

    render() {
        return (
       <div>
           <div className='suggest_list'>
               <ul className="suggest_list_list">
                   <li> 用户：{this.state.userName}</li>
                   <li>手机号：{this.state.userPhone}</li>
                   <li>反馈时间：{this.state.userDate}</li>
               </ul>

               <p className="suggest_line"></p>
               <div className="suggest_list_dl">
                   {
                       this.addList()
                   }
               </div>
               <span style={{marginLeft:109,color:'#EF112B'}} ref={'contentPoint'}></span>
               <div style={!Util.authorityCheck("ticking")? {display: 'none'} :{}}>
                   <InputBox defaultvalute="请输入回复内容" titletext="意见回复" space="40px"
                             style={{width:670, height: 150,marginTop:23, borderColor: "#efefef", color: "#999", paddingRight: 0}}
                             ref={'content'} maxlength={200} multiline={true} />
               </div>
               <div style={!Util.authorityCheck("ticking")? {display: 'none'} :{}}>
                   <Button onClick={this.handleConfirm.bind(this)} type="primary" style={{background: '#36b3b3',border: 'none',float:'right',marginTop:10}} icon="plus">意见回复</Button>
               </div>
           </div>

       </div>
        )
    }


    //asdasdasd
    /*日期格式化*/
    dateFormat(time) {
        let zero = (n) => {
            return n = n >= 0 && n < 10 ? "0" + n : n;
        };
        let y = new Date(time).getFullYear();
        let m = new Date(time).getMonth()+1;
        let d = new Date(time).getDate();
        let h = new Date(time).getHours();
        let ms = new Date(time).getMinutes();
        return zero(y)+"-"+zero(m)+"-"+zero(d)+" "+zero(h)+":"+zero(ms);
        // return zero(y) + "-" + zero(m) + "-" + zero(d)
    }

}